﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CircuitBoardApplication.Classes;
using CircuitBoardApplication.Classes.Components;

namespace CircuitBoardApplication
{
    public partial class Form1 : Form
    {
        private CircuitBoard circuitBoard;

        private int cid = 0;

        int X, Y;

        //Begin and End connection IDs'
        int bConn, eConn;
        ComponentMain startPoint, endPoint;

        private String componentType = "NONE";
        private String componentValue = "NONE";

        public Form1()
        {
            InitializeComponent();
            circuitBoard = new CircuitBoard();
        }


        void c_MouseDown(object sender, MouseEventArgs e)
        {

        }

        void panel1_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        } 

        private void circuitBoardPanel_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

                try
                {
                    bool sinkExists = false;
                    if (componentType == "SINK")
                    {
                        foreach (ComponentMain c in circuitBoard.components)
                        {
                            if (c.Type == "SINK")
                            {
                                sinkExists = true;

                            }
                        }
                        if (sinkExists == false)
                        {
                            ComponentMain tempComponent = circuitBoard.GetComponent(e.X, e.Y);
                            if (tempComponent == null)
                            {
                                circuitBoard.AddComponent(cid, componentType, 1, e.X, e.Y, componentValue);
                            }
                        }
                        else MessageBox.Show("sink exists");
                    }
                    else
                    {

                        ComponentMain tempComponent = circuitBoard.GetComponent(e.X, e.Y);
                        if (tempComponent == null)
                        {
                            circuitBoard.AddComponent(cid, componentType, 0, e.X, e.Y, componentValue);
                        }

                    }
                    cid += 1;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    Refresh();
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                try
                {
                    ContextMenuStrip panelItems = new ContextMenuStrip();
                    ToolStripMenuItem panelItemsDelete = new ToolStripMenuItem("Delete");
                    ToolStripMenuItem panelBeginConnection = new ToolStripMenuItem("Begin connection");
                    ToolStripMenuItem panelEndConnection = new ToolStripMenuItem("End connection");
                    
                    ToolStripMenuItem panelChangeSourceValue = new ToolStripMenuItem("Change the value");
                    panelChangeSourceValue.Click += new EventHandler(changeSourceValue_event);
                    
                    //Assign event handlers
                    panelItemsDelete.Click += new EventHandler(deleteItem);
                    panelBeginConnection.Click += new EventHandler(beginConnection_event);
                    panelEndConnection.Click += new EventHandler(endConnection_event);

                    X = e.X;
                    Y = e.Y;
                    
                    //Add to main context menu
                    
                    if (circuitBoard.GetComponent(e.X, e.Y) is Source)
                    {
                        panelItems.Items.AddRange(new ToolStripItem[] { panelItemsDelete, panelBeginConnection, panelEndConnection, panelChangeSourceValue });
                    }
                    else
                        panelItems.Items.AddRange(new ToolStripItem[] { panelItemsDelete, panelBeginConnection, panelEndConnection });
                    //Assign to datagridview
                    circuitBoardPanel.ContextMenuStrip = panelItems;
                    panelItems.Show(Cursor.Position);
                    
                    
                    //circuitBoard.RemoveComponent(e.X, e.Y);
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                //finally
                //{
                //    Refresh();
                //}

            }
        }

        private void changeSourceValue_event(object sender, EventArgs e)
        {

            if (circuitBoard.GetComponent(X, Y) is Source)
            {
                Source other;
                other = (Source)circuitBoard.GetComponent(X, Y);
                if (other.Value == 1)
                {
                    other.Value = 0;
                }
                else if (other.Value == 0)
                    other.Value = 0;
            }
            Refresh();

        }

        private void endConnection_event(object sender, EventArgs e)
        {
            endPoint = circuitBoard.GetComponent(X, Y);
            eConn = circuitBoard.GetComponent(X, Y).ID;
            circuitBoard.AddConnection(bConn, eConn);
            endPoint.addConnection(startPoint, endPoint);
            
            
            Refresh();
        }

        private void beginConnection_event(object sender, EventArgs e)
        {
            bConn = circuitBoard.GetComponent(X, Y).ID;
            startPoint = circuitBoard.GetComponent(X, Y);
        }

        private void deleteItem(object sender, EventArgs e)
        {
            circuitBoard.RemoveComponent(X, Y);
            Refresh();
        }

        private void btn_gateAND_Click(object sender, EventArgs e)
        {
            
            componentType = "GATE";
            componentValue = "AND";
        }

        private void btn_gateOR_Click(object sender, EventArgs e)
        {
            componentType = "GATE";
            componentValue = "OR";
        }

        private void btn_gateNOT_Click(object sender, EventArgs e)
        {
            componentType = "GATE";
            componentValue = "NOT";
        }

        private void circuitBoardPanel_Paint(object sender, PaintEventArgs e)
        {
            circuitBoard.DrawAllComponents(e.Graphics, this.imageList1);
            circuitBoard.DrawAllLines(e.Graphics);
        }

        private void btn_source_Click(object sender, EventArgs e)
        {
            componentType = "SOURCE";
            componentValue = "1";
        }

        private void btn_sink_Click(object sender, EventArgs e)
        {
            componentType = "SINK";
            componentValue = "true";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            foreach (ComponentMain c in this.circuitBoard.components)
            {
                listBox1.Items.Add(c.ToString());
            }
            listBox1.Items.Add("=======");
            foreach (ComponentMain c in circuitBoard.components)
            {
                //foreach (ComponentMain n in c.Neighbors)
                //{
                //    listBox1.Items.Add(n.ToString());
                //}
                
                foreach (Connection conn in c.Connections)
                {
                    listBox1.Items.Add(conn.ToString());
                    listBox1.Items.Add("---");
                    c.setInputsFromConnections(conn);

                }
            }
           
            //foreach(Connection conn in
            ////bandom pajungt srauta
            //ComponentMain comp = circuitBoard.GetComponent(X, Y);
            //// add cookies to all neighboors
            //List<ComponentMain> tempListOfPlates = comp.Neighbors;

            //foreach (ComponentMain c in tempListOfPlates)
            //{
            //    c.Calculate();
            //}



        }

        
    }
}
